// pages/barbecue/signup/index.js
var app=getApp();
import WxValidate from '../../../utils/WxValidate';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    options:{},
    aliyunOss: '',
    img_arr:[],
    idCard: '',
    username: '',
    mobile: '',
    userInfo:{},
    flag:true,
    idCardUpLoad:false,
    region: ['山西省', '太原市', '小店区'],
    customItem: '',
    post: []
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var dataset=options.dataset;
    var options=JSON.parse(dataset);
    this.setData({
      options: options,
      aliyunOss: app.data.aliyunOss
    })
    var userInfo=app.globalData.userInfo;
    var idCardUpLoad=userInfo.idCard;
    if (idCardUpLoad != undefined && idCardUpLoad.length>0){
         this.setData({
           idCardUpLoad:true
         })
    }
    if (userInfo === undefined || userInfo===''){
        wx.navigateTo({
          url: '/pages/login/index',
        })
    }else{
      var img_arr = [];
      userInfo.avatar ? img_arr.push(userInfo.avatar):'';
        this.setData({
          userInfo: userInfo,
          img_arr: img_arr
        })
    }
    wx.setNavigationBarTitle({
      title: options.objname,
    })
    this.initValidate();
  },
  clicksex:function(e){
    console.log(e);
    var sex=e.detail.value;
    var userInfo=this.data.userInfo;
    userInfo.sex=sex;
    this.setData({
      userInfo:userInfo
    })
  },
  initValidate() {
    const rules = {
      userName: {
        required: true,
        userName: true
      },
      mobile: {
        required: true,
        tel: true,
      },
      idCard: {
        required: true,
        idcard: true,
      },      
      parssword: {
        required: false,
        minlength: 6,
        maxlength: 16
      },
      sex:{
        required:true,
        assistance: true,
      },
      officeName:{
        required: true,
        minlength: 1,
      },
      post:{
        required: true,
        minlength: 1,
      },
      train: {
        required: true,
        assistance: true,
      }
    }
    const message = {
      userName: {
        required: '请输入真实姓名',
        rangelength: '请输入真实姓名'
      },
      mobile: {
        required: '请输入11位手机号码',
        tel: '手机号不能少于和大于11位',
      },
      idCard:{
        required: '请输入身份证号码',
        idcard: '请输入正确的身份证号码',
      },      
      parssword: {
        required: '请输密码',
        minlength: '密码长度不能少于6位',
        maxlength: '密码长度不能超过16位'
      },
      officeName: {
        required: '请输入所属单位',
        minlength: '不能为空',
      },
      post: {
        required: '请选择您的岗位',
        minlength: '不能为空',
      },
      train:{
        required:'请选择培训类型',
        assistance:'请选择培训类型',
      }
    }
    this.WxValidate = new WxValidate(rules, message);
    this.WxValidate.addMethod('userName', (value, param) => {
      return /^[\u4E00-\u9FA5]{2,4}$/.test(value)
    }, '请输入真实姓名');
    this.WxValidate.addMethod('sex',(value,paramm) => {
      return this.WxValidate.optional(value)||(value.length==1)
    },'请选择性别');
    this.WxValidate.addMethod('train',(value,param)=>{
      return this.WxValidate.optional(value)||(value.length==1)
    },'请选择培训类型')
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  selectpost:function(e){
    var datalist = e.currentTarget.dataset.datalist;
    var _this=this;
    app.paiRequest("/app/user/post/findList", {       
    }).then(function (res) {
      if (datalist != undefined) {
        if (datalist === 'post') {
          _this.setData({
            flag: false,
            datalist: res.data
          })
        }
      } 
    });
    
         
  },
  conceal:function(){
      this.setData({
        flag:true
      })
  },
  selectValue:function(e){
    console.log(e);
    var dataValue=e.detail.value;
    var userInfo=this.data.userInfo;
    var postName = dataValue.join(',')
    userInfo.postName = postName;
    this.setData({
      userInfo: userInfo
    })

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var userInfo = app.globalData.userInfo;
    if (userInfo === undefined || userInfo === '') {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  removeImg: function (e) {
    
    var index = e.currentTarget.dataset.index;
    var arry = this.data.img_arr;
    var _this = this;
    app.paiRequest('/app/moveFile', { filePath: arry[index] }).then(function (res) {
      _this.setData({
        img_arr: []
      })
    });

  },
  upimg:function(){
    var _this = this;
    var aliyunOss=this.data.aliyunOss;
    if (this.data.img_arr.length < 1) {
      wx.chooseImage({
        sizeType: ['original', 'compressed'],
        success: function (res) {
          console.log(res)
          var tempFilePaths = res.tempFilePaths;
          wx.uploadFile({
            url: app.data.service_url + '/app/uploadfile',
            filePath: tempFilePaths[0],
            name: 'file',
            header: {
              "Content-Type": "multipart/form-data"
            },
            success: function (res) {
              var data = JSON.parse(res.data);
              var code = data.code;
              var arry = data.data;
              var imag_arr = _this.data.img_arr;
              console.log(arry)
              imag_arr[0] = aliyunOss + arry[0];
              if (code == "200") {
                _this.setData({
                  img_arr: imag_arr
                })
              } else {
                var message = data.message;
                wx.showToast({
                  title: message,
                  icon: 'none',
                  duration: 3000,
                  mask: false
                })
              }
            },
            fail: function (res) {
              wx.showToast({
                title: '请求失败',
                icon: 'none',
                duration: 3000,
                mask: false
              })
            }
          })
        },
      })
    } else {
      wx.showToast({
        title: '最多上传三张图片',
        icon: 'loading',
        duration: 3000
      })
    }
  },
  showModal:function(message){
    console.log(message);
    wx.showToast({
      title: message.msg,
      duration: 2000,
      icon: 'none'
    }); 
  },
  submintData:function(e){
    const params = e.detail.value;
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0];
      this.showModal(error);
      return false;
    }
    console.log(params);
    var submitObj={
      username: params.userName,
      sex:params.sex,
      idCard: params.idCard,
      officeName: params.officeName,
      headOffice: this.data.userInfo.headOffice,
      postName: params.post,
      mobile: params.mobile,
      train: params.train,
      remarks: params.remarks,
      userPhoto: this.data.img_arr[0],
      barbecueId: this.data.options.id,
      barbecueTitle: this.data.options.objname,
      region: this.data.region
    };
    app.paiRequest('/app/barbecue/signup/add', submitObj).then(function (res) {
     
      wx.switchTab({
        url: '/pages/barbecue/index',
      })   
    })
  },
  inputusername:function(e){
    var username=e.detail.value;
    this.setData({
      username: username
    })
  },
  inputidcard:function(e){
    var datain = e.detail.value;
    this.setData({
      idCard: datain
    })
  },
  inputmobile:function(e){
    console.log(e)
    var datain = e.detail.value;
    this.setData({
      mobile: datain
    })
  },
   inputidcard: function (e) {
    var userInfo=this.data.userInfo;
     userInfo.idCard=e.detail.value;
    this.setData({
      userInfo: userInfo
    })
  },
})